##################################                                
IP Script      
##################################

; IP locator v1.0 - entropy dalnet

;============================== MENUS

menu menubar,status,channel,query {
  -
  Search Ip { locate }
  -
}

;============================== DIALOGS

dialog locate {
  title "Search Ip"
  size -1 -1 134 93
  option dbu notheme
  text "IP Address", 1, 4 4 26 8, right
  edit "", 2, 32 2 62 11
  button "Search", 3, 96 2 34 11
  text "Status", 4, 4 16 25 8, right
  edit "", 5, 32 14 62 11, read
  text "City", 6, 4 34 25 8, right
  text "State", 7, 4 43 25 8, right
  text "Country", 8, 4 52 25 8, right
  text "ISP", 9, 4 61 25 8, right
  text "", 10, 32 34 98 8
  text "", 11, 32 43 98 8
  text "", 12, 32 52 98 8
  text "", 13, 32 61 98 8
  button "Copy", 14, 62 78 34 11
  button "Close", 15, 96 78 34 11, ok
  box "", 16, -4 25 142 4
  box "", 17, -3 70 142 4
}

;============================== ALIASES

alias -l locate {
  if ($1) {
    did -ra locate 5 Search began..
    did -r locate 10,11,12,13
    sockclose locate
    sockopen locate www.melissadata.com 80
    sockmark locate $1-
  }
  else {
    dialog $iif($dialog(locate),-x,-m) locate locate
  }
}
alias -l striphtml {
  var %txt = $mid($1-,1,800)
  var %p = 1, %b = $replace(%txt,$chr(38),$+($chr(32),$chr(38)),;,$+(;,$chr(32)),>,$+(>,$chr(32)),<,$+($chr(32),<,)), %c
  while ($gettok(%b,%p,32)) {
    %c = $ifmatch
    if (*<* iswm %c || *&*; iswm %c || *</* iswm %c || *solid* iswm %c || *px* iswm %c || *:* iswm %c && $remove(%c,:) !isnum || */* iswm %c || *'* iswm %c || *;* iswm %c || *&* iswm %c || *<*>* iswm %c || *=* iswm %c || *"* iswm %c || *>* iswm %c || $+(*,$chr(40),*) iswm %c || $+(*,$chr(41),*) iswm %c || $+(*,$chr(123),*) iswm %c || $+(*,$chr(125),*) iswm %c) { %b = $remove(%b,%c) | %p = 0 }
    inc %p
  }
  return %b
}
alias -l endlocate {
  did -ra locate 5 Search completed..
  if (%ip) { did -ra locate 10 %city | did -ra locate 11 %state | did -ra locate 12 %country | did -ra locate 13 %isp }
  else { did -r locate 10,11,12,13 | did -ra locate 5 Found. }
  unset %isp
  unset %ip
  unset %city
  unset %state
  unset %country
  unset %isp
}

;============================== EVENTS

on *:dialog:locate:sclick:14:{
  var %a = IP: $did(2).text City: $did(10).text State: $did(11).text Country: $did(12).text ISP: $did(13).text
  clipboard %a
}
on *:dialog:locate:sclick:3:{
  if (!$did(2).text) { beep | return }
  locate $did(2).text
}
on *:sockopen:locate:{
  if ($sockerr > 0 && $dialog(locate)) { did -ra locate 5 Socket Open Error(s). | did -r locate 10,11,12,13 | return }
  sockwrite -n $sockname GET $+(/lookups/iplocation.asp?ipaddress=,$sock($sockname).mark) HTTP/1.1
  sockwrite -n $sockname Host: $+(www.melissadata.com,$str($crlf, 2))
  sockwrite -n $sockname Connection: keep-alive
}
on *:sockread:locate:{
  if ($sockerr > 0 && $dialog(locate)) { did -ra locate 5 Socket Read Error(s). | did -r locate 10,11,12,13 | return }
  var %q
  sockread %q
  tokenize 32 $striphtml(%q)
  did -ra locate 5 Data Reading..
  if (* IP Address *.*.*.* iswm $1-) { %ip = $4 }
  if (* City * iswm $1-) { %city = $3- }
  if (* State or Region * iswm $1-) { %state = $5- }
  if (* Country * iswm $1-) { %country = $3- }
  if (* ISP * iswm $1-) { %ISP = $3- }
  .timertrace -o 1 1 endlocate $sock($sockname).mark
}
